﻿/** 
 * Loads the XML  and dispatches the onLoad Event
 * call the file to load 
 * <code> xml = XMLDataLoader("myFile.xml");
 *	xml.addEventListener("onLoaded",myHandler);
 * </code>
 * <p>
 *
 * Event will be dispatched once the XML been loaded.
 *
 * @param path URL for the XML to be loaded
 *
 * XMLDataLoader is (c) 2009 Gil Beyruth
 *
 * @author Gil Beyruth <http://www.midiadesigners.com.br>
 *
 */

package com.firstpixel.utils
{
	import flash.display.Sprite;	
	import flash.display.DisplayObjectContainer;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.events.EventDispatcher;
	import flash.events.Event;
	
	public final class XMLDataLoader extends Sprite
	{
		
		public var XMLData:XML;
		protected static var disp:EventDispatcher;
		
		/**
		*
		* Add event listener.
		*
		*/
		public static function addEventListener(p_type:String, p_listener:Function, p_useCapture:Boolean=false, p_priority:int=0, p_useWeakReference:Boolean=false):void {
			if ( disp == null ) { disp = new EventDispatcher(); }
			disp.addEventListener(p_type, p_listener, p_useCapture, p_priority, p_useWeakReference);
		}
		/**
		*
		* Remove event listener.
		*
		*/
		public static function removeEventListener(p_type:String, p_listener:Function, p_useCapture:Boolean=false):void {
			if ( disp == null ) { return; }
			disp.removeEventListener(p_type, p_listener, p_useCapture);
		}
		/**
		*
		* Event dispatcher.
		*
		*/
		public static function dispatchEvent(p_event:Event):void {
			if ( disp == null ) { return; }
			disp.dispatchEvent(p_event);
		}
		/**
		*
		* XMLDataLoaded Events.
		*
		*/
  		public function XMLDataLoaded(evt:Event):void {
			XMLData = new XML( evt.target.data );
   			dispatchEvent( new Event("onLoad") );
   		}
		/**
		*
		* Class constructor.
		*
		*/
		public function XMLDataLoader(path:String):void
		{		
					var loader:URLLoader = new URLLoader();
					loader.addEventListener ( Event.COMPLETE , XMLDataLoaded );
					loader.load ( new URLRequest( path ) );
		}
	}
	
}